use crate::{config::AppContext, domain::widget::Widget, ApiResponse};
use axum::{response::IntoResponse, Form};

pub async fn save(widget: Form<Widget>) -> impl IntoResponse {
    let executor = AppContext::rbatis();
    if widget.id.unwrap_or_default() == 0 {
        let _ = Widget::insert(executor, &widget).await;
    } else {
        let _ = Widget::update_by_column_skip(executor, &widget, "id", true).await;
    }
    ApiResponse::from("操作成功".to_string())
}

pub async fn del(widget: Form<Widget>) -> impl IntoResponse {
    let widget = widget.0;
    assert!(widget.id.unwrap_or_default() > 0, "参数错误");
    let _ = Widget::delete_by_column(AppContext::rbatis(), "id", widget.id.unwrap()).await;
    ApiResponse::from("操作成功".to_string())
}
